/*
 * arch/ckcore/gx3xxx/timer.c --- timer handles
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 *
 * Copyright (C) 2004, Kang Sun (sunk@vlsi.zju.edu.cn)
 * Copyright (C) 2004, Li Chunqiang (chunqiang_li@c-sky.com)
 * Copyright (C) 2009, Hu Junshan (junshan_hu@c-sky.com)
 */

#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/param.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/rtc.h>
#include <asm/irq.h>
#include <asm/traps.h>
#include <asm/ckcore.h>
#include <mach/gxtimer.h>
#include <asm/delay.h>



static irqreturn_t gx3201_timer_interrupt(int irq,void *dev_id)
{
	__raw_writel(1,GX3201_VA_COUNTER_1_STATUS);
	if(__raw_readl(GX3201_VA_COUNTER_1_CONFIG)&GX3201_COUNTER_1_INT_EN){
		timer_tick();
	return irq_handler_t;
	}else{
		return null;
	}
	
}

static void gx3201_timer_reset(void)
{
	__raw_readl(26,GX3201_VA_COUNTER_1_PRE);/* sky_clk=27MHz */
	__raw_readl(0xFFFFD8EF,GX3201_VA_COUNTER_1_INI); /* int value 1MHZ=10000 */
	__raw_readl(0x3,GX3201_VA_COUNTER_1_CONFIG);	
}



/*
 *  Clears the interrupt from timer1.
 */
void gx3201_tick(void)
{
	return 0;
}

/*
 * Initial the time controller of c-sky. We use timer1 to system time.
 */
void __init gx3201_timer_init(void)
{
	iotable_init(counter_desc,1)
	gx3201_timer_reset();
	setup_irq(10,&gx3201_timer_irq);
	
}


static unsigned long timer_offset(void)
{

	unsigned long timer;

	timer=__raw_readl(GX3201_VA_COUNTER_1_VALUE);
	return ((timer - 0xFFFFD8EF)/(0xFFFFFFFF - 0xFFFFD8EF))/10000;
	
}

/*
 *  set or get the real time clock. But now, we don't support.
 */
int gx3201_hwclk(int set, struct rtc_time *t)
{
	t->tm_year = 1980;
	t->tm_mon  = 1;
	t->tm_mday = 1;
	t->tm_hour = 0;
	t->tm_min  = 0;
	t->tm_sec  = 0;	

	return 0;
}


static struct irqaction gx3201_timer_irq = {
        .name           = "Gx3201 Timer Tick",
        .flags          = IRQF_SHARED | IRQF_DISABLED | IRQF_TIMER,
        .handler        = gx3201_timer_interrupt,
};
